package com.learning.Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapDemo {
    public static void main(String[] args) {
        Map<String,Object> countryMap = new HashMap<>();
        countryMap.put("CN","中国");
        countryMap.put("JP","日本");
        countryMap.put("US","美国");
        countryMap.put("DE","德国");
        countryMap.put("MY","马来西亚");
        //第一种遍历HashMap的方法
        for (String key:countryMap.keySet()){
            Object value=countryMap.get(key);
            System.out.println("键是:"+key+",值是"+value);
        }
        System.out.println("***********************************************************");
        //第二种
        Iterator<Map.Entry<String,Object>> entryIt = countryMap.entrySet().iterator();
        while (entryIt.hasNext()){
            Map.Entry<String,Object>entry = entryIt.next();
            System.out.println("键是:"+entry.getKey()+",值是:"+entry.getValue());
        }
        System.out.println("***********************************************************");
        //第三种 Java8中使用forEach进行遍历
        countryMap.forEach((key,value)-> System.out.println("键是:"+key+",值是:"+value));

    }
}
